var e = getApp(),
    c = e.requirejs("api/index"),
  r = e.requirejs("core");
Page({
  data: {
    route: "member_index",
    menu: e.tabBar,
    menu_show: false,
    icons: e.requirejs("icons"),
    member: {},
    page:{},
    show: false,
    config: e.config,
    background: e.config.background,
    default_pic: '/yb_shop/static/images/add_pic.jpg',
  },
  //底部导航跳转
  menu_url: function(k) {
    r.menu_url(k, 2);
  },

  //跳转+收集表单id
  formSubmit: function (k) {
    var vv = { currentTarget: { dataset: {} } },
      data = k.detail.value;
    vv.currentTarget.dataset = data;
    r.menu_url(vv, 1);

    var d = {}
    d.formid = k.detail.formId;
    d.openid = getApp().getCache("userinfo").openid;
    d.username = getApp().getCache("userinfo").nickName;
    r.get('Market/getFormid', d, function (t) {
      console.log(t)
    })
  },

  //跳转
  to_url:function(k){
    r.menu_url(k, 1);
  },
  
  onLoad: function(options) {
    r.compatible_phonex(this);
    if (options != null && options != undefined) {
      this.setData({
        tabbar_index: options.tabbar_index ? options.tabbar_index : -1
      });
    }

    var up = getApp().getCache('user_center_page');
    if(up != "")
    {
      this.setData({
        page: up
      });
      this.initpage();
    }

    "" == e.getCache("userinfo") && (r.toast('您还没登录呢'),
      setTimeout(function() {
        wx.redirectTo({
          url: "/yb_shop/pages/index/index"
        })
      }, 2e3));
    var that = this;
    getApp().get_menu(function(x) {
      that.setData({
        menu: getApp().tabBar
      })
      getApp().check_is_tabbar();
      r.get('Distribe/shareSetting', {}, function (t) {
        if (t.code == 0) {
          that.setData({
            shareSetting: t.info
          })
        }
      })


      that.get_candan();

    })

  },

  initpage:function()
  {
    var that = this;
    wx.setNavigationBarColor({
      frontColor: that.data.page.text_color,
      backgroundColor: that.data.page.nv_color,
      animation: {
        duration: 0,
        timingFunc: "easeIn"
      }
    });

    wx.setNavigationBarTitle({
      title: that.data.page.name ? decodeURIComponent(that.data.page.name) : '首页'
    })

    if (wx.setBackgroundColor) {
      wx.setBackgroundColor({
        backgroundColor: that.data.page.bg_color ? that.data.page.bg_color : '#f2f3f4'
      })
    }

  },

  get_candan: function() {
    var that=this;
    c.indexMod(that, function (t) {
      console.log(t);
      that.setData({
        list: t.index,
        show: t.show,
        page: t.page,
      });
      getApp().setCache('user_center_page', t.page);
      that.initpage();
      that.getInfo()
    },'ucenter');
  },

  formBook: function (e) {
    var that = this,
      data = e.detail.value,
      uid = getApp().getCache("userinfo").uid;
    if (data.name == '') {
      r.error('姓名不能为空');
      return;
    } else if (uid == null) {
      r.error('请先登录');
      setTimeout(function () {
        r.jump('/yb_shop/pages/index/index', 2);
      }, 1000)
      return;
    } else if (data.phone.length != 11) {
      r.error('手机号格式不正确');
      return;
    }
    data.user_id = uid;
    r.get('Index/WriteBook', data, function (t) {
      if (t.code == 0) {
        that.setData({
          bookData: {}
        });
        r.success('提交成功');
      } else {
        r.alert(t.msg);
      }
    })

  },
  /**
   * 下拉刷新
   */
  onPullDownRefresh: function() {
    this.getInfo()
    wx.stopPullDownRefresh();
  },
  //获取统计信息
  getInfo: function() {
    var e = this;
    e.setData({
      background: getApp().config.background,
      font_color: getApp().config.font_color
    })
    r.get("user/Index", {
      uid: getApp().getCache("userinfo").uid
    }, function(r) {
      console.log(r)
      r.info.nickName = getApp().getCache("userinfo").nickName,
        r.info.avatarUrl = getApp().getCache("userinfo").avatarUrl
      console.log(r)
      e.setData({
        member: r.info
      })
    })
  },
  fail: function() {
    wx.redirectTo({
      url: "/yb_shop/pages/message/auth/index"
    })
  },
  onShow: function() {
    this.getInfo();
  },
  relogin: function() {
    var that = this;
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']) {
          wx.openSetting({
            success: (res) => {
              if (res.authSetting['scope.userInfo']) {
                e.getUserInfo();
                setTimeout(function() {
                  that.getInfo()
                }, 1e3)

              }
            }
          })
        }
      }
    })

  },

  /**
     * 地图
     */
  navigate: function (e) {
    var t = a.pdata(e);
    if (t.name && t.lat && t.lng) {
      a.tx_map(t.lat, t.lng, t.name)
    } else {
      a.toast('不能获取到该位置')
    }
  },
  phone: function (t) {
    r.phone(t)
  },
  // 万能表单提交
  formPower: function (e) {
    c.formSubmit(this, e);
  },

  bindPickerChange: function (e) {
    c.bindPickerChange(this, e);
  },
  listen_time_two: function (e) {
    c.listen_time_two(this, e);
  },
  chooseImageTap1: function (e) {
    var id = r.pdata(e).id;
    c.upload(this, id, 'form_pic', 1)
  },
  chooseImageTap2: function (e) {
    var id = r.pdata(e).id;
    c.upload(this, id, 'form_pic', 0)
  },
  Image_del: function (e) {
    c.Image_del(this, e);
  },
  /**
   * 图片预览
   */
  previewImage: function (e) {
    var url = a.pdata(e).url,
      arr = a.pdata(e).arr;
    r.previewImage(url, arr, 'imgurl')
  },



})